#ifndef WORKER_H
#define WORKER_H

#include "command.h"
#include "log.h"
#include "serialportsync.h"

#include <QMutex>
#include <QObject>
#include <QRunnable>
#include <qthread.h>
#include "global.h"

class Worker : public QThread
{
    Q_OBJECT
public:
    Worker(QObject *parent = nullptr);
    float getTemperature();
    float getWeight();
    float getPh();
    float getPhTemperature();
    float getPhUsageTime();
    float getConductivity();
    float getConductivityTemperature();
    float getConductivityUsageTime();
    float getSpeed();
    void    addCmd(Command cmd);
    Command getCmd();

    void  sendCmd(Command cmd);
    bool  sendData(SerialPortSync& port, char* rawData);
    void  setRun(bool run);

signals:
    void signalUpdate(float speed, float temperature, float weight, float ph, float phTemp, float phUsageTime,
                      float conductivity, float conTemp, float conUsage);
    void signalInfo(Global::Cmd cmd, Global::Result result, QString info);

public slots:
    void slotUpdateParam(const QString& param, const QString& value);

protected:
    void run() override;
private:
    bool init(QString& err);
    float setRound(float value);
private:
    QAtomicInteger<int> m_logPeriodMillionSecond;
    QAtomicInteger<int> m_weightUnitDecimalPlaces;

    SerialPortSync *m_pSerialTemperature;
    SerialPortSync *m_pSerialWeight;
    SerialPortSync *m_pSerialSpeed;
    SerialPortSync *m_pSerialPh;
    SerialPortSync *m_pSerialConductivity;
    // Log             m_log;

    QVector<Command>        m_vector;
    QMutex                  m_mutex;
    QAtomicInteger<bool>    m_collection;
};

#endif // WORKER_H
